﻿using Microsoft.WindowsAzure.Storage.Blob;
using PetkaWebDav.Server.Framework.BaseClasses;
using PetkaWebDav.Server.Framework.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core.WebDav.Handlers
{
    /// <summary>
    /// Custom implementation example for DavGet.
    /// </summary>
    public sealed class DavGet : DavGetBase
    {
        BlobItem _fileInfo = null;

        public DavGet()
        {
        }


        protected async override Task OutputStream(Stream outStream)
        {
            //HttpApplication.Response.Headers["Content-Type"] = _fileInfo.ContentType;
            //HttpApplication.Response.Headers["Last-Modified"] = AzureUtil.ConvertFromDateTimeOffset(_fileInfo.UploadDate).ToString("r");

            await RequestWrapper.DownloadBlob(_fileInfo.Path, outStream);
        }
        protected override async Task OnProcessDavRequest(EventArgs e)
        {
            try
            {
                _fileInfo = await RequestWrapper.DownloadBlobAttributes(this.GetRelativeRequestPath());
            }
            catch (Exception ex)
            {
                base.AbortRequest(ServerResponseCode.NotFound);
            }

            if (_fileInfo == null)
                base.AbortRequest(ServerResponseCode.NotFound);
        }
    }
}
